package cn.zd.demo1.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import cn.zd.demo1.R;

public class MyDialog extends Dialog implements View.OnClickListener {
    private String title,msg,cancel,ok;
    private OnCancelListener cancelListener;
    private OnOkListener okListener;
    private TextView tv_title;
    private TextView tv_msg;
    private TextView tv_cancel;
    private TextView tv_ok;
    private Context context;

    public MyDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public MyDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_mydialog);
        // 设置宽度
        WindowManager manager = getWindow().getWindowManager();
        Display display = manager.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        Point size = new Point();
        display.getSize(size);
        p.width = (int)(size.x * 0.8);
        getWindow().setAttributes(p);

        tv_title = findViewById(R.id.tv_title);
        tv_msg = findViewById(R.id.tv_msg);
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_ok = findViewById(R.id.tv_ok);
        if(!TextUtils.isEmpty(title)) {
            tv_title.setText(title);
        }
        if(!TextUtils.isEmpty(msg)) {
            tv_msg.setText(msg);
        }
        if(!TextUtils.isEmpty(cancel)) {
            tv_cancel.setText(cancel);
        }
        if(!TextUtils.isEmpty(ok)) {
            tv_ok.setText(ok);
        }
        tv_cancel.setOnClickListener(this);
        tv_ok.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.tv_cancel) {
            if(cancelListener != null) {
                cancelListener.onCancel(this);
            }else {
                Toast.makeText(context,"没有设置该cancen事件",Toast.LENGTH_SHORT).show();
            }
            dismiss();
        }else if(v.getId() == R.id.tv_ok) {
            if(okListener != null) {
                okListener.onOK(this);
            }else {
                Toast.makeText(context,"没有设置该ok事件",Toast.LENGTH_SHORT).show();
            }
            dismiss();
        }
    }

    public interface  OnCancelListener{
        void onCancel(MyDialog myDialog);
    }
    public interface  OnOkListener{
        void onOK(MyDialog myDialog);
    }
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCancel() {
        return cancel;
    }

    public void setCancel(String cancel,OnCancelListener onCancelListener) {
        this.cancel = cancel;
        this.cancelListener = onCancelListener;
    }

    public String getOk() {
        return ok;
    }

    public void setOk(String ok,OnOkListener okListener) {
        this.ok = ok;
        this.okListener = okListener;
    }
}
